package com.kg.sdk.adapt;

import android.content.Context;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.kpgamesdk.R;

import java.util.List;

public class KPGiftAdapter extends ArrayAdapter<KPGiftData> {
    private GiftClickListener mGiftClickListener;
    public KPGiftAdapter(Context context,  List<KPGiftData> objects) {
        super(context, 0, objects);
    }
    public void setGiftClickListener(GiftClickListener listener){
        mGiftClickListener = listener;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        ViewHolder viewHolder = null;
        if(convertView != null){
            viewHolder = (ViewHolder) convertView.getTag();
        }else{
            convertView = inflater.inflate(R.layout.adp_use_item, null);
            viewHolder = new ViewHolder();
            viewHolder.initView(convertView);
        }
        KPGiftData data = getItem(position);
        viewHolder.tvDesc.setText(data.desc);
        viewHolder.tvGiftStyleName.setText(data.name);
        viewHolder.tvDesc.setText(data.desc);
        viewHolder.tvGiftStyle.setText(data.tagName);
        viewHolder.tvGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mGiftClickListener != null) {
                    mGiftClickListener.onGiftClick(position);
                }
            }
        });
        return convertView;
    }

    public static class ViewHolder{
        public TextView tvGiftStyle;
        public TextView tvGiftStyleName;
        public TextView tvDesc;
        public TextView tvGet;
        public ViewHolder(){

        }
        public void initView(View view){
            tvGiftStyle = (TextView)view.findViewById(R.id.tvGiftStyle);
            tvGiftStyleName = (TextView)view.findViewById(R.id.tvGiftStyleName);
            tvDesc = (TextView)view.findViewById(R.id.tvDesc);
            tvGet = (TextView)view.findViewById(R.id.tvGet);
        }
    }

    public interface GiftClickListener{
        public void onGiftClick(int position);
    }
}
